//
//  TFGuideInterfaceView.m
//  引导界面
//
//  Created by sunrise-OYR on 16/10/24.
//  Copyright (c) 2016年 Liwy. All rights reserved.
//

#import "BDGuideInterfaceView.h"
#import "OpenInstallSDK.h"

#define TF_GUIDE_INTERFACE_IMAGEDATA_COUNT _imageDataArray.count
@interface BDGuideInterfaceView () <UIScrollViewDelegate> {
    NSMutableArray *_imageDataArray;
}

@property (nonatomic, strong)UIButton *enterButton;           // 进入主页按钮
@property (nonatomic, strong)UIScrollView *scrollView;        // 滚动视图
@property (nonatomic, strong)UIPageControl *pageControl;      // 分页指示器
@property (nonatomic, strong)NSArray <NSString *>* imageTopArr;
@property (nonatomic, strong)NSArray <NSString *>* imageCtrArr;
@end

@implementation BDGuideInterfaceView

- (instancetype)initWithFrame:(CGRect)frame imageDataArray:(NSArray *)imageDataArray {
    self = [super initWithFrame:frame];
    if (self) {
        [self initializeDatasourceWithImaegDataArray:imageDataArray];
        [self initializeUserInterface];
        [self getInstallParamsImmediately];
    }
    return self;
}

- (void)initializeDatasourceWithImaegDataArray:(NSArray *)imageDataArray {
    _imageDataArray = [[NSMutableArray alloc] initWithArray:imageDataArray];
}
-(NSArray <NSString *>*)imageCtrArr {
    if (!_imageCtrArr) {
        _imageCtrArr = @[@"guidectr1",@"guidectr2",@"guidectr3",@"guidectr4"];
    }
    return _imageCtrArr;
}
-(NSArray <NSString *>*)imageTopArr {
    if (!_imageTopArr) {
        _imageTopArr = @[@"guidetop1",@"guidetop2",@"guidetop3",@"guidetop4"];
    }
    return _imageTopArr;
}

- (void)initializeUserInterface {
    
    // 背景视图
    UIView *backGroundView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGTH)];
    backGroundView.backgroundColor = [UIColor blackColor];
    
    [self addSubview:backGroundView];
    
    self.scrollView = ({
        UIScrollView *scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGTH)];
        scrollView.contentSize = CGSizeMake(TF_GUIDE_INTERFACE_IMAGEDATA_COUNT * SCREEN_WIDTH, SCREEN_HEIGTH);
        scrollView.contentOffset = CGPointZero;
        scrollView.delegate = self;
        scrollView.pagingEnabled = YES;
        [backGroundView addSubview:scrollView];
        scrollView.showsHorizontalScrollIndicator = NO;
        scrollView;
    });
    
    // 添加图片视图
    for (int i = 0; i <TF_GUIDE_INTERFACE_IMAGEDATA_COUNT; i++) {
        UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(i * SCREEN_WIDTH, 0, SCREEN_WIDTH, SCREEN_HEIGTH)];
//        imageView.image = [UIImage imageNamed:_imageDataArray[i]];
        imageView.backgroundColor = [UIColor whiteColor];
//        imageView.contentMode = UIViewContentModeScaleAspectFit;
        [_scrollView addSubview:imageView];
        
        UIImageView * topView = [UIImageView new];
        topView.image = [UIImage imageNamed:self.imageTopArr[i]];
        [imageView addSubview:topView];
        
        UIImageView * ctrImageView = [UIImageView new];
        ctrImageView.image = [UIImage imageNamed:self.imageCtrArr[i]];
        [imageView addSubview:ctrImageView];
        
        
        [topView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.centerX.equalTo(imageView);
            make.top.equalTo(@103);
            make.size.mas_equalTo(CGSizeMake(topView.image.size.width, topView.image.size.height));
        }];
        
        [ctrImageView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.centerX.equalTo(imageView);
            make.top.equalTo(topView.mas_bottom).offset(73);
            make.size.mas_equalTo(CGSizeMake(ctrImageView.image.size.width, ctrImageView.image.size.height));
        }];
        
        if (i==self.imageTopArr.count-1) {
            imageView.userInteractionEnabled = YES;
            UITapGestureRecognizer * tap = [[UITapGestureRecognizer alloc]init];
            @weakify(self)
            [[tap rac_gestureSignal] subscribeNext:^(__kindof UIGestureRecognizer * _Nullable x) {
                @strongify(self)
                [self enterButtonPressed];
            }];
            [imageView addGestureRecognizer:tap];
            
            UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
            [imageView addSubview:button];
//            button.frame = CGRectMake(0, 0,SCREEN_WIDTH, SCREEN_HEIGTH);
            [button mas_makeConstraints:^(MASConstraintMaker *make) {
                make.top.equalTo(ctrImageView.mas_bottom).offset(63);
                make.centerX.equalTo(imageView);
                make.size.mas_equalTo(CGSizeMake(167, 47));
            }];
            [[button rac_signalForControlEvents:UIControlEventTouchUpInside] subscribeNext:^(__kindof UIControl * _Nullable x) {
                @strongify(self)
                [self enterButtonPressed];
            }];
            [button setTitle:@"立即体验" forState:0];
            button.titleLabel.font = [UIFont fontWithName:PingFangSC_Medium size:21];
            [button setBackgroundColor:[UIColor colorWithHex:0xFF2400]];
//            [button addTarget:self action:@selector(enterButtonPressed) forControlEvents:UIControlEventTouchUpInside];
        }
    }
    
    // 按钮
//    self.enterButton = ({
//        UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
//        button.bounds = CGRectMake(0, 0, SCREEN_W / 2, 50);
//        button.center = CGPointMake((TF_GUIDE_INTERFACE_IMAGEDATA_COUNT - 1)*SCREEN_W + SCREEN_W / 2, SCREEN_H - 50 * HRadio);
//        
//        [button addTarget:self action:@selector(enterButtonPressed:) forControlEvents:UIControlEventTouchUpInside];
//        [_scrollView addSubview:button];
//        
//        button;
//    });
    
    self.pageControl = ({
        UIPageControl *pageControl = [[UIPageControl alloc] init];
        pageControl.bounds = CGRectMake(0, 0, SCREEN_WIDTH, 35);
        pageControl.center = CGPointMake(SCREEN_WIDTH / 2, SCREEN_HEIGTH - 35);
        pageControl.numberOfPages = TF_GUIDE_INTERFACE_IMAGEDATA_COUNT;
        pageControl.pageIndicatorTintColor = [UIColor darkGrayColor];
        pageControl.currentPageIndicatorTintColor = [UIColor colorWithHex:0xF91C03];
        
        [self addSubview:pageControl];
        
        pageControl;
    });
}

- (void)enterButtonPressed {
    [[NSUserDefaults standardUserDefaults] setObject:@"1" forKey:@"isGuideShow"];
    [[NSUserDefaults standardUserDefaults] synchronize];
    NSInteger index = _scrollView.contentOffset.x / SCREEN_WIDTH;
    if (index < TF_GUIDE_INTERFACE_IMAGEDATA_COUNT - 1) {
        [_scrollView setContentOffset:CGPointMake(SCREEN_WIDTH * (index + 1), 0) animated:YES];
        return;
    }
    [UIView animateWithDuration:0.4 animations:^{
        [UIView setAnimationCurve:UIViewAnimationCurveLinear];
        self.alpha = 0.3;
    } completion:^(BOOL finished) {
        [self removeFromSuperview];
    }];
}

- (void)scrollViewDidScroll:(UIScrollView *)scrollView {
    _pageControl.currentPage = scrollView.contentOffset.x / SCREEN_WIDTH;
}
//通过OpenInstall获取已经安装App被唤醒时的参数（如果是通过渠道页面唤醒App时，会返回渠道编号）
-(void)getInstallParamsImmediately {
    [[OpenInstallSDK defaultManager] getInstallParmsCompleted:^(OpeninstallData*_Nullable appData) {
        NSString * url = [Api stringByAppendingString:ShareCallbackApi];
        NSMutableDictionary * dic = [NSMutableDictionary dictionary];
        [dic addEntriesFromDictionary:appData.data];
        [HJHttpTool get:url params:dic success:^(HJResponeds *result) {
            LKLog(@"%@",result.data);
        } failure:^(id result) {
            LKLog(@"%@",result);
        }];
    }];
}

@end





































